package graphAlg.gen;

import graphAlg.Graph;
import graphAlg.GraphGenerator;
import graphAlg.Node;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;

public class Graph_DFS {
    public static void main(String[] args) {
        Graph graph = GraphGenerator.createGenerateGraph(new int[][]{
                {1, 2, 3},
                {1, 3, 4},
                {1, 4, 5},
                {2, 3, 6},
                {3, 4, 7},
                {2, 5, 8},
                {3, 5, 9},
                {4, 6, 10},
                {6, 7, 11}
        });
        dfs(graph.getNodes().get(2));
    }

    public static void dfs(Node node){
        Stack<Node> stack = new Stack<>();
        Set<Node> set = new HashSet<>();
        stack.push(node);
        System.out.println(node);
        set.add(node);
        while(!stack.isEmpty()){
            Node curNode = stack.pop();
          for(Node nextNode : curNode.nexts){
              if(!set.contains(nextNode)){
                  stack.push(curNode);
                  stack.push(nextNode);
                  System.out.println(nextNode);
                  set.add(nextNode);
                  break;
              }
          }
        }
    }
}
